package accounting.action;

import accounting.model.Cost;
import accounting.model.Income;
import accounting.service.CostService;
import accounting.service.IncomeService;
import com.opensymphony.xwork2.ActionSupport;
import manage.model.CostType;
import manage.model.IncomeType;
import manage.service.CostTypeService;
import manage.service.IncomeTypeService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuweigui
 */
@SuppressWarnings("serial")
public class CostAction extends ActionSupport {

    private List<Cost> costs = new ArrayList<Cost>();

    private String costTypesJSON;

    private CostService costService;
    private CostTypeService costTypeService;
    private Cost cost = new Cost();
    //for ajax call 
    //result="success" if successful, otherwise error message content
    private String result;

    public String delete() {
        costService.remove(cost.getId());
        return SUCCESS;
    }

    public String execute() {
        costs = costService.findAll();

        List<CostType> costTypes = costTypeService.findAll();
        StringBuilder sb = new StringBuilder();
        sb.append("costTypes:[");
        for (CostType costType : costTypes) {
            sb.append(costType.toJSON()).append(",");
        }
        sb.append("]");

        return SUCCESS;
    }

    public String add() {
        costService.add(cost);
        return SUCCESS;
    }

    public List<Cost> getCosts() {
        return costs;
    }

    public void setCosts(List<Cost> costs) {
        this.costs = costs;
    }

    public String getCostTypesJSON() {
        return costTypesJSON;
    }

    public void setCostTypesJSON(String costTypesJSON) {
        this.costTypesJSON = costTypesJSON;
    }

    public CostService getCostService() {
        return costService;
    }

    public void setCostService(CostService costService) {
        this.costService = costService;
    }

    public CostTypeService getCostTypeService() {
        return costTypeService;
    }

    public void setCostTypeService(CostTypeService costTypeService) {
        this.costTypeService = costTypeService;
    }

    public Cost getCost() {
        return cost;
    }

    public void setCost(Cost cost) {
        this.cost = cost;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }


}
